﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Transformer.iHunter.Plugins.BBS
{
    public partial class BbsItemViewer : UserControl, Transformer.iHunter.IMessageViewer
    {
        public BbsItemViewer()
        {
            InitializeComponent();
        }

        #region IMessageViewer Members

        public void DisplayMessage(IMessage message)
        {            
            if (Mail.MailCategory.Equals(message.Category))
            {
                Mail mail = new Mail(message);

                this.pictureBox_Icon.Image = global::Transformer.iHunter.Plugins.BBS.Properties.Resources.BBSMail;
                label_Type.Text = "BBS Mail";
                checkBox_ShowMailInfo.Visible = true;

                if (checkBox_ShowMailInfo.Checked)
                    panel_MailInfo.Size = new Size(panel_MailInfo.Size.Width, 120);
                else
                    panel_MailInfo.Size = new Size(panel_MailInfo.Size.Width, 38);

                label_Sender.Text = "Sender:    " + mail.Sender;
                label_Receiver.Text = "Receiver:  " + mail.Receiver;
                label_Time.Text = "SendTime:  "+mail.SendTime.ToString("yyyy/MM/dd");                
                label_MailTitle.Text = "Title:     "+mail.Title;

                webBrowser_Content.DocumentText = mail.HtmlText;
                textBox_Content.Text = mail.PlainText.Replace("\n",System.Environment.NewLine);
            }
            else
            {
                this.pictureBox_Icon.Image = global::Transformer.iHunter.Plugins.BBS.Properties.Resources.BBSPost;
                label_Type.Text = "BBS Post";
                checkBox_ShowMailInfo.Visible = false;

                panel_MailInfo.Size = new Size(panel_MailInfo.Size.Width, 0);
                                
                webBrowser_Content.DocumentText = message.Content;
                if (message.CustomFields.ContainsKey("PlainText"))
                    textBox_Content.Text = message.CustomFields["PlainText"].Replace("\n", System.Environment.NewLine);
            }
        }


        public System.Xml.XPath.XPathNavigator ContextualCommands
        {
            get { return null; }
        }

        public void ExecuteCommand(string name, System.Xml.XPath.XPathNavigator arg)
        {
            
        }

        #endregion

        private void checkBox_ShowMailInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_ShowMailInfo.Checked)
                panel_MailInfo.Size = new Size(panel_MailInfo.Size.Width, 120);
            else
                panel_MailInfo.Size = new Size(panel_MailInfo.Size.Width, 38);
        }

		#region IMessageViewer Members


		public void ExcuteCommand(CommandUIEventArgs uiElement)
		{
			// Do nothing
		}

		public event CommandUIEventHandler UpdateCommandUI;

		#endregion

        private void textBox_Content_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                //textBox_Content.SelectedText = System.Environment.NewLine;
                e.Handled = true;
            }
        }
	}
}
